#include <iostream>
#include <string>
#include <stack>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp;
Node* H = nullptr;
void getTree(Node*& h)
{
	string cur;
	cin >> cur;
	if (cur == sp) return;
	h = new Node;
	h->val = cur;
	getTree(h->left);
	getTree(h->right);
}
void prev(Node* h)
{
	if (!h) return;
	stack<Node*> s;
	cout << h->val;
	s.push(h->right);
	s.push(h->left);
	while (s.size()) {
		auto cur = s.top(); s.pop();
		if (cur)
		{
			cout << ',' << cur->val;
			s.push(cur->right);
			s.push(cur->left);
		}
	}
}
int main()
{
	cin >> sp;
	getTree(H);
	prev(H);
	return 0;
}